package com.yum.Iterator.Arrays;

import org.omg.CORBA.Object;

import java.lang.reflect.Array;

@SuppressWarnings("all")
public class YArrays {


    public static char[] copyOf(char[] original,int newLength){
        char[] copy = new char[newLength];
        System.arraycopy(original, 0, copy, 0,
                Math.min(original.length, newLength));
        return copy;
    }


    public static <T> T[] copyOf(T[] original,int newLength){
        return (T[]) copyOf(original,newLength,original.getClass());
    }

    public static <T,E> T[] copyOf(E[] original,int newLength,Class<? extends T[]> newType){
        //创建指定长度的某种类型的数组
        T[] copy = ((java.lang.Object) newType==(java.lang.Object) java.lang.Object[].class)
                ? (T[])new Object[newLength] : (T[]) Array.newInstance(newType.getComponentType(),newLength);
        //调用System.arraycopy方法
        System.arraycopy(original, 0, copy, 0,
                Math.min(original.length, newLength));

        //byte[] copy = new byte[newLength]; 此语句可以替换上面创建数组的代码

        return copy;
    }



}
